package main.java.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import ssm.base.SeeStudent;
import ssm.dao.IUserDao;
import ssm.pojo.Page;
import ssm.pojo.User;
import ssm.service.IUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author: silence
 * Date: 17-5-26
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Autowired
    private IUserDao userDao;

    /**
     * 在首页显示所有用户的信息
     *
     * @param model
     * @return
     */

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String listAllUser(Model model,HttpServletRequest request) {
        request.getSession().getAttribute("login");
        List<User> userList = userService.findAllByQuery();
        model.addAttribute("userList", userList);
        return "redirect:/user/queryAll?pageNow=1";
    }

    /**
     * 显示一个用户的信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/users/{id}")
     public String toIndex(@PathVariable Integer id, Model model){
        model.addAttribute("user", userService.getUserById(id));
        return "update";
    }

    /***
     * 添加用户信息
     * @param model
     * @return
     */
    //链接到add页面时是GET请求，会访问这段代码，注册表单时先判断用户
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(Model model) {
        //开启modelDriven
        model.addAttribute(new User());
        return "add";
    }

    //进入用户注册界面判断用户名是否已经存在，使用user.getUserName方法
    //在具体添加用户时，是post请求，就访问以下代码
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(@Validated User user,HttpSession session)  {
        User u = userService.findUserByUsername(user.getUsername());
        if (u!=null){
            session.setAttribute("username",user.getUsername());
            return "addError";
        }

        userService.addUser(user);
        return "addSuccess";
    }

    /***
     * 更新一个用户信息
     */

    @RequestMapping(value="/update",method=RequestMethod.POST)
    public String update(@Validated User user){
        User user1=userService.getUserById(user.getUserId());
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setDescription(user.getDescription());
       userService.updateByPrimaryKey(user1);
        return "updateSuccess";
    }


    /**
     * 删除一个用户信息
     * */
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable Integer id,Model model){
        userService.deleteByPrimaryKey(id);
        model.addAttribute("usern", id);
        return "delete";
    }

    /***
     * 通过关键字查询得出用户的详细信息
     * @param request
     * @param model
     * @return
     */

    @RequestMapping(value = "/findUser",method = RequestMethod.POST)//

    public String findUsersByName(HttpServletRequest request,Model model){

        String name=request.getParameter("name");

        List<User> users=userService.findUsersByName(name);

        model.addAttribute("users",users);

        return "research";

    }

    @RequestMapping(value = "/findUser",method = RequestMethod.GET)//
    public String findUsers(){
        return "research";
    }


    /*查看操作记录*/
    @RequestMapping("/queryAll")
    public String record(String loginName , Model model, SeeStudent seeStudent, String pageNow){
        String user_Name;
        Page page = null;//new 对象在下面
        user_Name = loginName;//获取当前登录的用户的loginName
        int totalCount = userDao.getCount();//获取当前用户总记录条数
        if(pageNow != null && pageNow != "") {//防止出现空指针异常
            int pageNow1 = Integer.parseInt(pageNow);
            page = new Page(totalCount, pageNow1);//这样写的好处，判断完成外面可以继续调用
        }
        seeStudent.setLoginName(user_Name);
        seeStudent.setPage(page);
        List<User> recordList = userDao.queryAllRecord(seeStudent);//取得当前用户的所有操作记录
        model.addAttribute("page", page);
        model.addAttribute("recordList",recordList);
//        return "recordPage";
        return "showAllUser";


    }

}
